package com.abel.bigwater

import com.abel.bigwater.data.BcdString
import io.netty.buffer.ByteBuf
import kotlin.math.min

object PacketHelper {
    /**
     * consume byte-array with extent to
     * @see ByteBuf
     */
    fun ByteBuf.rawArray(_len: Int? = null): ByteArray {
        _len ?: return ByteArray(this.readableBytes()).also {
            this.readBytes(it)
        }

        return ByteArray(min(_len, this.readableBytes())).also {
            this.readBytes(it)
        }
    }

    /**
     * consume byte-array with extent to
     * @see ByteBuf
     */
    fun ByteBuf.hexString(_len: Int? = null, _withBlank: Boolean = false): String =
            BcdString.hexString(this, min(_len ?: Int.MAX_VALUE, this.readableBytes()), _withBlank)
}